const DEFAULT_PAGE = 0;
//获取应用实例
const app = getApp()
// 云调用
const db = wx.cloud.database()
Page({
  data: {
    Tlength: '', //科普新闻总数
    Text: '', //科普文章数组
    textnum: 4, //科普文章加载数
    stateD: false, //底线标志控制
    activeres: '',
    // 屏幕移动量数组
    toView: `card_${DEFAULT_PAGE}`,
    list: [],
    //动态Tab
    title: ['主页', '发现'],
    active: 1,
    index: 0,
    //公共变量
    itemnum: 0,
    item: 0,
    bgitemimg: '',
    //图片数据
    logo: 'https://636c-cloud2-9gomot6xb9909364-1305292962.tcb.qcloud.la/%E8%93%9D%E5%A4%A9%E7%A7%91%E6%99%AE3.0%E5%AD%98%E5%82%A8/index/Logo.png?sign=f265df88381fa0907b75e467357c8b9e&t=1646620340',
    fontimg: 'https://636c-cloud2-9gomot6xb9909364-1305292962.tcb.qcloud.la/%E8%93%9D%E5%A4%A9%E7%A7%91%E6%99%AE3.0%E5%AD%98%E5%82%A8/index/font.png?sign=90a2e348bf392b4ee6756079b1a55c05&t=1647502594',
    itemchange: 'cloud://cloud2-9gomot6xb9909364.636c-cloud2-9gomot6xb9909364-1305292962/蓝天科普3.0存储/index/主题更换.png',
    //科普板块
    tabplate: [{
        img: 'cloud://cloud2-9gomot6xb9909364.636c-cloud2-9gomot6xb9909364-1305292962/蓝天科普3.0存储/index/时钟.png',
        text: '科普预约',
        color1: '#b16eec',
        color2: '#a6d5e6',
      },
      {
        img: 'cloud://cloud2-9gomot6xb9909364.636c-cloud2-9gomot6xb9909364-1305292962/蓝天科普3.0存储/index/趋势.png',
        text: '科普平台',
        color1: '#EC6EAD',
        color2: '#3494E6',
      },
      {
        img: 'cloud://cloud2-9gomot6xb9909364.636c-cloud2-9gomot6xb9909364-1305292962/蓝天科普3.0存储/index/文件.png',
        text: '科普书屋',
        color1: '#FBD786',
        color2: '#f7797d',
      },
      {
        img: 'cloud://cloud2-9gomot6xb9909364.636c-cloud2-9gomot6xb9909364-1305292962/蓝天科普3.0存储/index/饼图.png',
        text: '科普世界',
        color1: '#E684AE',
        color2: '#79CBCA',
      }
    ],
  },
  onLoad: function (e) {
    //云开发调用
    db.collection('active').where({
      type_id: "active01"
    }).get({
      success: res => {
        //console.log(res.data)
        // res.data 是包含以上定义的两条记录的数组
        this.setData({
          activeres: res.data
        })
      }
    })
    db.collection('news').where({
      type_id: 'news01'
    }).get({
      success: res => {
        if (res.data != '') {
          var length = res.data.length - 1 //科普文章总数目
          this.setData({
            Tlength: length
          })
          // console.log(length, res.data)
          if (length < 5) {
            switch (length) {
              case 1:
                var text1 = [res.data[0]]
                this.setData({
                  Text: text1
                });
                break;
              case 2:
                var text2 = [res.data[0], res.data[1]]
                this.setData({
                  Text: text2
                });
                break;
              case 3:
                var text3 = [res.data[0], res.data[1], res.data[2]]
                this.setData({
                  Text: text3
                });
                break;
              case 4:
                var text4 = [res.data[0], res.data[1], res.data[2], res.data[3]]
                this.setData({
                  Text: text4
                });
                break;
            }
          } else {
            var text = [res.data[0], res.data[1], res.data[2], res.data[3]]
            this.setData({
              Text: text
            });
          }
        } else {
          this.setData({
            Text: '',
          })
        }
        //console.log('科普文章列表输出', text)
      }
    })
  },
  setchange: function (o) {
    let activeID = o.currentTarget.dataset.index
    //console.log(activeID)
    this.setData({
      active: activeID,
    })
  },
  setspot: function (u) {
    var id = u.currentTarget.dataset.spot
    switch (id) {}
  },

  touchStart(e) {
    this.startPageX = e.changedTouches[0].pageX;
  },
  startPageX: 0,
  currentView: DEFAULT_PAGE,
  touchEnd(e) {
    const moveX = e.changedTouches[0].pageX - this.startPageX;
    const maxPage = this.data.list.length - 1;
    if (Math.abs(moveX) >= 100) {
      if (moveX > 0) {
        this.currentView = this.currentView !== 0 ? this.currentView - 1 : 0;
      } else {
        this.currentView = this.currentView !== maxPage ? this.currentView + 1 : maxPage;
      }
    }
    this.setData({
      toView: `card_${this.currentView}`
    });
  },
  //板块获取及跳转
  getplate: function (res) {
    var id = res.currentTarget.dataset.id
    switch (id) {
      case 0:
        wx.navigateTo({
          url: '/zpages/orders/orders',
        });
        break;
      case 1:
        wx.showToast({
          title: '暂未开发',
          icon: 'error',
          duration: 2000,
          mask: true
        });
        break;
      case 2:
        wx.navigateTo({
          url: '/views/book/book',
        });
        break;
      case 3:
        wx.navigateTo({
          url: '/zpages/world/world',
        });
        break;
    }
    //console.log(id)
  },
  //页面上拉触底事件的处理函数
  onReachBottom: function () {
    var that = this
    let active = that.data.active
    let number = that.data.textnum
    let Tlength = that.data.Tlength
    //console.log(number)
    if (number < Tlength) {
      if (active == 0) {
        wx.showLoading({
          title: '刷新中',
          icon: "loading",
          duration: 1000,
        })
        db.collection('news').where({
          type_id: 'news01'
        }).get({
          success: res => {
            var length = res.data.length //科普文章总数目
            var array = [] //初始化文章数组
            if (length > number) {
              var numx = number + 2
              // console.log(numx)
              for (var i = 0; i < numx; i++) {
                const item = res.data[i]
                array.push(item)
                setTimeout(function () {
                  //console.log(i, array) //要延时执行的代码
                }, 1000) //延迟时间 这里是1秒
              }
              this.setData({
                Text: array,
                textnum: numx,
              })
            } else {
              this.setData({
                stateD: true
              })
            }
          }
        })
        //console.log('下拉输出', number)
      } else {
        //console.log('发现页面暂无操作')
      }
    } else {
      this.setData({
        stateD: true
      })
    }
  },
  //主题更换函数
  setitem: function (item) {
    let that = this
    var itemnums = that.data.itemnum + 1
    switch (itemnums) {
      case 1:
        this.setData({
          item: 1,
          itemnum:itemnums,
          bgitemimg:'cloud://cloud2-9gomot6xb9909364.636c-cloud2-9gomot6xb9909364-1305292962/轮番图/pic2.jpg'
        });
        break;
      case 2:
        this.setData({
          item: 1,
          itemnum:itemnums,
          bgitemimg:'cloud://cloud2-9gomot6xb9909364.636c-cloud2-9gomot6xb9909364-1305292962/轮番图/pic1.jpg'
        });
        break;
      case 3:
        this.setData({
          item: 0,
          itemnum:0,
        });
        break;
    }
    //console.log(itemnums)
  },
    //科普文章跳转
    goToarticl: function (e) {
      //获取携带data-id的数据
      console.log(e)
      let id = e.currentTarget.dataset.id
      //携带ID进行页面跳转
      wx.navigateTo({
        url: '/zpages/articl/articl?id='+id,
      })
    },
  //生命周期函数--监听页面初次渲染完成
  onReady: function () {

  },

  //生命周期函数--监听页面显示
  onShow: function () {

  },

  //生命周期函数--监听页面隐藏
  onHide: function () {

  },

  //生命周期函数--监听页面卸载
  onUnload: function () {

  },

  //页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh: function () {

  },

  // 用户点击右上角分享
  onShareAppMessage: function () {

  }
})